package com.freemarker.demo.controller;

import com.freemarker.demo.pojo.User;
import com.freemarker.demo.utils.ServerResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @CLASSNAME FreemarkerController
 * @DESCRIPTION 描述
 * @AUTHOR chen
 * @DATE 2019-03-22 9:42
 **/

@Controller
public class FreemarkerController {

    @GetMapping("/index")
    public String goIndex(Model model) {

        List <String> list = new ArrayList <String>();
        list.add( "java" );
        list.add( "python" );
        list.add( "nodejs" );
        model.addAttribute( "myList", list );

        Map <String, String> map = new HashMap <String, String>();
        map.put( "java", "你好Java" );
        map.put( "python", "你好python" );
        map.put( "nodejs", "你好nodejs" );
        model.addAttribute( "map", map );

        model.addAttribute( "user", "chen" );
        return "index";
    }

    @GetMapping("/echart")
    @ResponseBody
    public Map <String, Object> echart() {

        List <ChartEntity> entityList = new ArrayList <ChartEntity>();
        ChartEntity chartEntity1 = new ChartEntity( "西瓜销售额", "bar", Arrays.asList( 100, 300, 200, 150 ) );
        ChartEntity chartEntity2 = new ChartEntity( "苹果销售额", "bar", Arrays.asList( 200, 200, 100, 250 ) );
        ChartEntity chartEntity3 = new ChartEntity( "香蕉销售额", "bar", Arrays.asList( 300, 100, 600, 50 ) );
        ChartEntity chartEntity4 = new ChartEntity( "栗子销售额", "bar", Arrays.asList( 320, 120, 620, 52 ) );
        entityList.add( chartEntity1 );
        entityList.add( chartEntity2 );
        entityList.add( chartEntity3 );
        entityList.add( chartEntity4 );

        String[] legend = {"西瓜销售额", "苹果销售额", "香蕉销售额", "栗子销售额"};

        String[] xAxis = {"一月", "二月", "三月", "四月"};

        Map <String, Object> map = new HashMap <String, Object>();
        map.put( "xAxis", xAxis );
        map.put( "legend", legend );
        map.put( "series", entityList );

        return map;
    }

    @GetMapping("/dot")
    @ResponseBody
    public ServerResponse dot(@RequestParam(value = "pn", defaultValue = "1") int pn, @RequestParam(value = "pageSize", defaultValue = "3") int pageSize) {

        PageHelper.startPage( pn, pageSize );
        List <User> userList = new ArrayList <>();
        User u1 = new User( 1, "chen", 22, "note" );
        User u2 = new User( 2, "hello", 22, "note" );
        User u3 = new User( 3, "deng", 22, "note" );
        User u4 = new User( 4, "carry", 22, "note" );
        User u5 = new User( 5, "tom", 22, "note" );

        userList.add( u1 );
        userList.add( u2 );
        userList.add( u3 );
        userList.add( u4 );
        userList.add( u5 );
        PageInfo p = new PageInfo( userList );

        return ServerResponse.createBySuccess( p );
    }
}
